package com.example.testdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.testdemo.util.EmulationUtilKt;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

public class MainActivity2 extends AppCompatActivity {

    private static final String TAG = "MainActivity2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        findViewById(R.id.test_x86).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isEmulation = EmulationUtilKt.isEmulatorByX86(getApplicationContext());
                showIsEmulation(isEmulation);
            }
        });

        findViewById(R.id.test_file_load).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isEmulation = EmulationUtilKt.isEmulatorByFolderExists();
                showIsEmulation(isEmulation);
            }
        });

        findViewById(R.id.test_tell_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityCompat.checkSelfPermission(MainActivity2.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity2.this, new String[]{Manifest.permission.READ_PHONE_STATE},200);
                    return;
                }
                boolean isEmulation =  EmulationUtilKt.isEmulatorByTellPhone(getApplicationContext());
                showIsEmulation(isEmulation);
            }
        });
        findViewById(R.id.test_light_bluetooth).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isEmulation =  EmulationUtilKt.isEmulatorByHasLightOrBluetooth(getApplicationContext());
                showIsEmulation(isEmulation);
            }
        });
        findViewById(R.id.test_check_cpu_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isEmulation =  EmulationUtilKt.isEmulatorByCpuName();
                showIsEmulation(isEmulation);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 200){
           if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
               boolean isEmulation =  EmulationUtilKt.isEmulatorByTellPhone(getApplicationContext());
               showIsEmulation(isEmulation);
           }
        }
    }

    /**
     * 展示是否模拟器的提示
     * @param isEmulation
     */
    private void showIsEmulation(boolean isEmulation){
        Toast.makeText(getApplicationContext(),"是否为模拟器"+isEmulation,Toast.LENGTH_SHORT).show();
        Log.i(TAG, "是否为模拟器"+isEmulation);
    }


    public  int[]  main(int i) {
        int[] in = new int[]{0,0,0,0,0};
        if (i>=10000){
            in[0] = i/10000;
        }
        if (i>=1000){
            in[1] = (i%10000)/1000;
        }
        if (i>=100){
            in[2] = (i%1000)/100;
        }
        if (i>=10){
            in[3] =(i%100)/10;
        }
            in[4] = i%10;
        System.out.println(in);
        return in;
    }

    private void getTag(String tag){
      String[] tagsplit =  tag.split("#");
      Log.i(TAG,tagsplit[1]+"");
    }

    private void clearTag(String tag){
        Log.i(TAG,"清除前的："+tag);
        ArrayList<String> strings = getLabStrLists(tag);
        //清除正文中的标签
        for (int i = 0;i<strings.size();i++){
            tag =   tag.replace(strings.get(i),"");
        }
        Log.i(TAG,"清除后的:"+tag);
    }

    /**
     * 将数据正文中的标签数据获取出来 并进行拼接到数组中
     *
     * @param text
     */
    private ArrayList<String> getLabStrLists(String text) {
        ArrayList<String> mTObjectsList = new ArrayList<>();// object集合
        mTObjectsList.clear();
        int textLength = text.length();
        int formIndex = 0;
        List<Integer> indexs = new ArrayList<>();
        while (formIndex <= textLength) {
            formIndex = text.indexOf("#", formIndex);
            if (formIndex == -1) {
                break;
            }
            indexs.add(formIndex);
            formIndex++;
        }
        for (int i = 0; i < indexs.size(); ) {
            if (i + 1 < indexs.size()) {
                String substring = text.substring(indexs.get(i), indexs.get(i + 1) + 1);
                if (TextUtils.isEmpty(substring)) continue;
                if (substring.length() <= 22 && substring.length() > 2) {
                    i = i + 2;
                    if (!mTObjectsList.contains(substring)) {
                        mTObjectsList.add(substring);
                    }
                } else {
                    i = i + 1;
                }
            } else {
                i = indexs.size();
            }
        }
        return mTObjectsList;
    }


    private void getwithAndHeight() {
        int[] wh = new int[2];
        String url =  "https://img.q17z.com/Flo0cl8rxSR7_LKULKxMkgshfhsg?imageslim&imgWidth=4288.0&imgHeight=2848.0";
        String[] spliWs = url.split("imgWidth=");
        if (spliWs.length > 0){
           String s = spliWs[spliWs.length-1];
           String[] sp = s.split("&");
           wh[0] = Double.valueOf(sp[0]).intValue();
        }
        String[] spliHs = url.split("imgHeight=");
        if (spliHs.length>0){
          String h = spliHs[spliHs.length-1];
          wh[1] = Double.valueOf(h).intValue();
        }
        Log.i(TAG,"宽度："+wh[0]+" 高度："+wh[1]);
    }

    public static String getTime(long second) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String hms = formatter.format(second);
        return hms;
    }
}